<?php
/**
 * File containing the ActualDateProcessor
 *
 * PHP Version 5
 *
 * @category Processors
 * @package  Processors
 * @author   Martin Schlecht <herrlollinger@gmail.com>
 * @license  MIT License
 * @link     http://code.google.com/p/universal-test-data
 */

/**
 * Processor class "ActualDateProcessor"
 *
 * Processor which is used to process tags which implements actual dates within the text.
 *
 * @category   Processors
 * @package    Processors
 * @subpackage DateProcessors
 * @author     Martin Schlecht <herrlollinger@gmail.com>
 * @copyright  see @author
 * @license    MIT License
 * @link       http://code.google.com/p/universal-test-data
 *
 * The followings are the available attributes of the processor class
 * @property string $text, the text which should be processed
 */
class ActualDateProcessor
{
	private $text;
	
	/**
	 * The processor adds the actual date or datetime
	 *
	 * @param String $text, the text to process
	 *
	 * @return String
	 */
	public function process($text) {
		$this->text = $text;
		
		preg_match_all("#\s*{(.*?)}#si", $this->text, $matches);
		
		foreach ($matches[1] AS $m) {
			$string = "{" . $m . "}";
			$m = explode(":", $m);
			
			/**
			 * {actualDate:-} 2012-03-10
			 * {actualDate:/} 10/03/2012
			 * {actualDate:.} 10.03.2012
			 * {actualDate:}  10032012
			 * {actualDate} 20120310
			 * Actual date with given layout
			 */
			if ($m[0] == "actualDate") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || $parameter[0] == "") {
					$parameter[0] = "dmY";
				} else if ($parameter[0] == "r") {
					$parameter[0] = "Ymd";
				} else if ($parameter[0] == "-") {
					$parameter[0] = "Y-m-d";
				} else if ($parameter[0] == "/") {
					$parameter[0] = "d/m/Y";
				} else if ($parameter[0] == ".") {
					$parameter[0] = "d.m.Y";
				} else {
					$parameter[0] = "Y-m-d";
				}
				
				$time = time();
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = 0;
				}
				
				$time += $parameter[1] * 60*60*24;
				
				if (!isset($parameter[2]) || !is_numeric($parameter[2])) {
					$parameter[2] = 1;
				}
				
				$replacement = date($parameter[0], $time);
			
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[2]), $this->text);
			}
			
			/**
			 * {actualDateTime:-} 2012-03-10 13:40:00
			 * {actualDateTime:/} 10/03/2012 13:41:00
			 * {actualDateTime:.} 10.03.2012 13:42:00
			 * {actualDateTime:}  10032012134300
			 * {actualDateTime} 20120310134400
			 * Actual date and time with preconfigured layout
			 */
			if ($m[0] == "actualDateTime") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || $parameter[0] == "") {
					$parameter[0] = "dmYHis";
				} else if ($parameter[0] == "r") {
					$parameter[0] = "YmdHis";
				} else if ($parameter[0] == "-") {
					$parameter[0] = "Y-m-d H:i:s";
				} else if ($parameter[0] == "/") {
					$parameter[0] = "d/m/Y H:i:s";
				} else if ($parameter[0] == ".") {
					$parameter[0] = "d.m.Y H:i:s";
				} else {
					$parameter[0] = "Y-m-d H:i:s";
				}
				
				$time = time();
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = 0;
				}
				
				$time += $parameter[1] * 60*60*24;
				
				if (!isset($parameter[2]) || !is_numeric($parameter[2])) {
					$parameter[2] = 0;
				}
				
				$time += $parameter[2] * 60;
				
				if (!isset($parameter[3]) || !is_numeric($parameter[3])) {
					$parameter[3] = 1;
				}
				
				$replacement = date($parameter[0], $time);
			
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[3]), $this->text);
			}
			
			/**
			 * {actualDateTimeLayout:Y} 2012
			 * Returns the actual date time in a given configuration which is given in the first parameter.
			 * For documentation see http://de.php.net/manual/de/function.date.php
			 */
			if ($m[0] == "actualDateTimeLayout") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[0]) || $parameter[0] == "") {
					$parameter[0] = "dmYHis";
				} 
				
				$time = time();
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = 0;
				}
				
				$time += $parameter[1] * 60*60*24;
				
				if (!isset($parameter[2]) || !is_numeric($parameter[2])) {
					$parameter[2] = 0;
				}
				
				$time += $parameter[2] * 60;
				
				if (!isset($parameter[3]) || !is_numeric($parameter[3])) {
					$parameter[3] = 1;
				}
				
				$replacement = date($parameter[0], $time);
			
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[3]), $this->text);
			}
			
			/**
			 * {actualUnixTimestamp:} 123456789
			 * Actual unix timestamp
			 */
			if ($m[0] == "actualUnixTimestamp") {
				$parameter = explode(",", $m[1]);
				
				$time = time();
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = 0;
				}
				
				$time += $parameter[1] * 60*60*24;
				
				if (!isset($parameter[2]) || !is_numeric($parameter[2])) {
					$parameter[2] = 0;
				}
				
				$time += $parameter[2] * 60;
				
				if (!isset($parameter[3]) || !is_numeric($parameter[3])) {
					$parameter[3] = 1;
				}
				
				$replacement = $time;
			
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[3]), $this->text);
			}
		}
		
		return $this->text;
	}
	
	/**
	 * Manages the repeation of values
	 * 
	 * @param String $value,  the value which should be replaced
	 * @param String $string, the string for the cockie
	 * @param String $times,  the number of times the value should be repeated
	 * 
	 * @return String
	 */
	public function manageRepeation($value, $string, $times) {
		$replacement = "";
		
		if (Yii::app()->session[$string . "_times"] >= $times || !isset(Yii::app()->session[$string . "_times"])) {
			Yii::app()->session[$string . "_replacement"] = $value;
			Yii::app()->session[$string . "_times"] = 1;
							
			$replacement = $value;
		} else {
			$number = Yii::app()->session[$string . "_replacement"];
			$times = Yii::app()->session[$string . "_times"] + 1;
			
			Yii::app()->session[$string . "_times"] = $times;
			
			$replacement = $number;
		}
		
		Yii::app()->session['resetManager']->add($string . "_replacement");
		Yii::app()->session['resetManager']->add($string . "_times");
		
		return $replacement;
	}
	
	/**
	 * Returns the documentation
	 *
	 * @return String
	 */
	 public function documentation() {
		return array(
			array('{actualDate:format,dayOffset}', 'Actual date with given layout'),
			array('{actualDateTime:format,dayOffset,hourOffset}', 'Actual date and time with preconfigured layout'),
			array('{actualDateTime:format,dayOffset,hourOffset}', 'Actual date and time with given layout'),
			array('{actualUnixTimestamp:}', 'Actual unix timestamp'),
		);
	 }
	 
	/**
	 * Prints the documentation
	 *
	 * @return string
	 */
	public function print_documentation() {
		foreach ($this->documentation() AS $docu) {
			echo '<span style="font-family:monospace;font-size:11px">' . $docu[0] . '</span><br/>' . $docu[1] . '<br/>';
		}
	}
}
?>